package com.example.core.thread;

import com.example.core.config.BeanContext;
import com.example.core.entity.AfterSaleIndex;
import com.example.core.entity.DealerForm;
import com.example.core.service.ProcessingService;
import com.example.core.utils.RegionEnum;
import com.example.core.utils.SearchEnum;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@Async("taskExecutor")
public class AfterSaleThread implements Callable<Map<String, Object>> {


    private final String quarterId;
    private final String yearId;
    private final Map<String, List<DealerForm>> baseMap;
    private ProcessingService processingService;

    public AfterSaleThread(String yearId, String quarterId, Map<String, List<DealerForm>> baseMap) {
        this.yearId = yearId;
        this.quarterId = quarterId;
        this.baseMap = baseMap;
    }

    @Override
    public Map<String, Object> call() throws Exception {

        this.processingService = BeanContext.getApplicationContext().getBean(ProcessingService.class);
        DealerForm dealerForm = new DealerForm();
        Map<String, Object> resultMap = new HashMap<>();
        AfterSaleIndex usedCountry = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.全国, SearchEnum.全国);
        AfterSaleIndex usedCountryTotal = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.全国, SearchEnum.全国全年累计);
        AfterSaleIndex usedEast = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.东区, SearchEnum.大区);
        AfterSaleIndex usedEastTotal = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.东区, SearchEnum.大区全年累计);
        AfterSaleIndex usedSouth = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.南区, SearchEnum.大区);
        AfterSaleIndex usedSouthTotal = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.南区, SearchEnum.大区全年累计);
        AfterSaleIndex usedWest = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.西区, SearchEnum.大区);
        AfterSaleIndex usedWestTotal = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.西区, SearchEnum.大区全年累计);
        AfterSaleIndex usedNorth = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.北区, SearchEnum.大区);
        AfterSaleIndex usedNorthTotal = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.北区, SearchEnum.大区全年累计);
        //大区由五个变四个，去掉中区
        //AfterSaleIndex usedCentre = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.中区, SearchEnum.大区);
        //AfterSaleIndex usedCentreTotal = processingService.getAfterSaleCollectIndex(yearId, quarterId, dealerForm, baseMap, RegionEnum.中区, SearchEnum.大区全年累计);

        List<AfterSaleIndex> afterDealer = new ArrayList<>();
        List<AfterSaleIndex> afterDealerTotal = new ArrayList<>();
        List<DealerForm> dealerList = baseMap.get("currentDealer");
        dealerList.forEach(o -> {
            afterDealer.add(processingService.getAfterSaleCollectIndex(yearId, quarterId, o, baseMap, RegionEnum.全国, SearchEnum.经销商));
            afterDealerTotal.add(processingService.getAfterSaleCollectIndex(yearId, quarterId, o, baseMap, RegionEnum.全国, SearchEnum.经销商全年累计));

        });

        List<AfterSaleIndex> area = new ArrayList<>();
        area.add(usedEast);
        area.add(usedSouth);
        area.add(usedWest);
        area.add(usedNorth);
        //area.add(usedCentre);
        List<AfterSaleIndex> areaTotal = new ArrayList<>();
        areaTotal.add(usedEastTotal);
        areaTotal.add(usedSouthTotal);
        areaTotal.add(usedWestTotal);
        areaTotal.add(usedNorthTotal);
        //areaTotal.add(usedCentreTotal);

        resultMap.put("售后_全国", usedCountry);
        resultMap.put("售后_全国累计", usedCountryTotal);
        resultMap.put("售后_东区", usedEast);
        resultMap.put("售后_东区累计", usedEastTotal);
        resultMap.put("售后_南区", usedSouth);
        resultMap.put("售后_南区累计", usedSouthTotal);
        resultMap.put("售后_西区", usedWest);
        resultMap.put("售后_西区累计", usedWestTotal);
        resultMap.put("售后_北区", usedNorth);
        resultMap.put("售后_北区累计", usedNorthTotal);
        //resultMap.put("售后_中区", usedCentre);
        //resultMap.put("售后_中区累计", usedCentreTotal);
        resultMap.put("售后_经销商", afterDealer);
        resultMap.put("售后_经销商累计", afterDealerTotal);
        resultMap.put("售后_区域", area);
        resultMap.put("售后_区域累计", areaTotal);
        return resultMap;
    }
}
